package com.gph.saviorframework.security.service.impl;

import com.gph.saviorframework.FieldType;
import com.gph.saviorframework.common.model.Field;
import com.gph.saviorframework.dao.security.FieldDao;
import com.gph.saviorframework.security.service.FieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class FieldServiceImpl implements FieldService {

    @Autowired
    private FieldDao fieldDao;

    private Map<String, String> fields = new HashMap<String, String>();

    public FieldServiceImpl(){
        fields.put("FILED_ID", FieldType.STRING);
        fields.put("FIELD_NAME", FieldType.STRING);
    }

    /**
     * 查询列表
     *
     * @param sort       排序字段
     * @param dir        排序方向
     * @param params     查询参数
     * @param functionId 功能编号
     * @return
     */
    public List<Map<String, Object>> find(String sort, String dir, Map<String, Object> params, String functionId) {
        String whereClause = genWhereClause(params);
        if(!StringUtils.isEmpty(functionId)){
            whereClause += " AND F.FUNCTION_id='"+functionId+"'";
        }
        return fieldDao.find(sort, dir, whereClause);
    }

    /**
     * 查询记录数
     *
     * @param params     查询参数
     * @param functionId 功能编号
     * @return
     */
    public long count(Map<String, Object> params, String functionId) {
        String whereClause = genWhereClause(params);
        if(!StringUtils.isEmpty(functionId)){
            whereClause += " AND F.FUNCTION_id='"+functionId+"'";
        }
        return fieldDao.count(whereClause);
    }

    /**
     * 保存
     *
     * @param field
     */
    public void save(Field field) {
        fieldDao.save(field);
    }

    /**
     * 根据编号获取
     *
     * @param id
     * @return
     */
    public Field get(Long id) {
        return fieldDao.get(id);
    }

    /**
     * 更新
     *
     * @param field
     * @return
     */
    public void update(Field field) {
        fieldDao.update(field);
    }

    /**
     * 批量更新
     *
     * @param fields
     */
    @Override
    public void update(Set<Field> fields) {
        for(Field field : fields){
            update(field);
        }
    }

    /**
     * 根据条件返回数据
     *
     * @param sort
     * @param dir
     * @param params
     * @return
     */
    public List<Map<String, Object>> find(String sort, String dir, Map<String, Object> params) {
        return find(sort,dir,params,"");
    }

    /**
     * 带分页参数的，给bootstrap使用的
     *
     * @param start  开始页码，从1开始
     * @param limit  每页查询的次数
     * @param sort
     * @param dir
     * @param params
     * @return
     */
    @Override
    public Map<String, Object> find(int start, int limit, String sort, String dir, Map<String, Object> params,Object... extras) {
        return null;
    }

    /**
     * 统计数量
     *
     * @param params
     * @return
     */
    public long count(Map<String, Object> params) {
        return count(params,"");
    }

    /**
     * 根据主键删除
     *
     * @param id
     */
    public void delete(Long id) {
        fieldDao.deleteById(id);
    }

    /**
     * 批量删除
     *
     * @param ids
     */
    public void deleteByIds(Long[] ids) {
        for(long id : ids){
            delete(id);
        }
    }

    /**
     * 查询字段集合
     *
     * @return
     */
    public Map<String, String> getFields() {
        return fields;
    }

    /**
     * 根据id查询map
     *
     * @param id
     * @return
     */
    public Map<String, Object> findById(Long id) {
        return fieldDao.findById(id);
    }

    public String genWhereClause(Map<String, Object> params,Object... extra) {
        String whereClause = "1=1";
        if (params!=null && params.keySet().size() > 0) {
            for (String key : params.keySet()) {
                if (fields.get(key).equals(FieldType.STRING)) {
                    whereClause += " and F." + key + " LIKE '"+params.get(key)+"'";
                } else {
                    whereClause += " and F." + key + " ='"+params.get(key)+"'";
                }
            }
        }

        return whereClause;
    }
}
